package mulegame;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;

/*
import mulegame.PlayerScreen.Listener;
*/

/**
 * Creates the Town Screen with players and different buying options. will add logic and actionlisteners later
 * @author Briam
 *
 */
    
public class TownScreen extends TimedScreen {
    private JLabel lblTimer;
    private int timeleft;
    private JTextArea txtrPlayer;
    
	/**
	 * Create the town screen with player scores, choices to enter the land office, pub, or store and a timer
	 * path images of different options with action listeners 
	 */
	public TownScreen(GraphicsControl ctrl, int duration) {
        super(ctrl, duration);
        
		setSize(new Dimension(900, 600));
		setBorder(new MatteBorder(4, 4, 4, 4, (Color) new Color(0, 0, 0)));
		setLayout(null);
		
		int numOfPlayers = control.getNumPlayers();
		
		JLabel lblMainTitle = new JLabel("Coffee Mule");
		lblMainTitle.setBounds(175, 11, 563, 51);
		lblMainTitle.setForeground(new Color(165, 42, 42));
		lblMainTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblMainTitle.setFont(new Font("Batang", Font.BOLD, 45));
		add(lblMainTitle);
		
		JLabel lblLogoSMALL = new JLabel("");
		lblLogoSMALL.setIcon(new ImageIcon("src/mulegame/Images/LogoSmall.jpg"));
		lblLogoSMALL.setBounds(20, 11, 97, 93);
		lblLogoSMALL.setBorder(new LineBorder(new Color(0, 0, 0), 2));
		add(lblLogoSMALL);
		
		lblTimer = new JLabel("XX Seconds");
		lblTimer.setForeground(Color.RED);
		lblTimer.setBorder(new LineBorder(Color.BLACK, 2));
		lblTimer.setFont(new Font("Times New Roman", Font.PLAIN, 29));
		lblTimer.setHorizontalAlignment(SwingConstants.CENTER);
		lblTimer.setBounds(708, 10, 168, 83);
        add(lblTimer);
		
		JButton btnReturn = new JButton(new BackToMapListener());
		btnReturn.setText("Return To Map");
		btnReturn.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
        btnReturn.setFont(new Font("Times New Roman", Font.PLAIN, 20));
		btnReturn.setBorderPainted(true);
		btnReturn.setBounds(697, 500, 168, 83);   
		add(btnReturn); 
		
		JButton btnLoadGame = new JButton(new LoadGameListener());
		btnLoadGame.setText("Load Saved Game");
		btnLoadGame.setFont(new Font("Times New Roman", Font.PLAIN, 20));
		btnLoadGame.setBorderPainted(true);
		btnLoadGame.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		btnLoadGame.setBounds(55, 500, 168, 40);
		add(btnLoadGame);
		
		JButton btnSaveGame = new JButton(new SaveGameListener());
		btnSaveGame.setText("Save Current Game");
		btnSaveGame.setFont(new Font("Times New Roman", Font.PLAIN, 20));
		btnSaveGame.setBorderPainted(true);
		btnSaveGame.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		btnSaveGame.setBounds(55, 543, 168, 40);
		add(btnSaveGame);
		
		JPanel townBorder = new JPanel();
		townBorder.setBorder(new TitledBorder(null, "Coffee Town",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		townBorder.setBounds(223, 83, 471, 295);
		add(townBorder);
		townBorder.setLayout(null);

		JPanel theTown = new JPanel();
		theTown.setBounds(10, 27, 450, 250);
		townBorder.add(theTown);
		theTown.setLayout(null);
		
		JButton button = new JButton("");
		button.setBounds(123, 25, 93, 76);
		button.setIcon(new ImageIcon("src/mulegame/Images/town1.jpg"));
		button.setBorderPainted(true);
		button.setBorder(new LineBorder(Color.BLACK, 2));
		button.addActionListener(new StoreAction());
		theTown.add(button);
		
		JButton button1 = new JButton("");
		button1.setIcon(new ImageIcon("src/mulegame/Images/town2.jpg"));
		button1.setBounds(234, 25, 93, 76);
		button1.setBorderPainted(true);
		button1.setBorder(new LineBorder(Color.BLACK, 2));
		button1.addActionListener(new PubAction());
		theTown.add(button1);
		
		JButton button2 = new JButton("");
		button2.setIcon(new ImageIcon("src/mulegame/Images/town3.jpg"));
		button2.setBounds(80, 174, 93, 76);
		button2.setBorderPainted(true);
		button2.setBorder(new LineBorder(Color.BLACK, 2));
		theTown.add(button2);
		
		JButton button3 = new JButton("");
		button3.setIcon(new ImageIcon("src/mulegame/Images/town4.jpg"));
		button3.setBounds(274, 174, 93, 76);
		button3.setBorderPainted(true);
		button3.setBorder(new LineBorder(Color.BLACK, 2));
		theTown.add(button3);
		
		JButton button5 = new JButton("");
		button5.setIcon(new ImageIcon("src/mulegame/Images/trees.JPG"));
		button5.setBorderPainted(true);
		button5.setBorder(new LineBorder(Color.BLACK, 2));
		button5.setBounds(0, 0, 124, 101);
		theTown.add(button5);
		
		JButton button6 = new JButton("");
		button6.setIcon(new ImageIcon("src/mulegame/Images/trees2.JPG"));
		button6.setBorderPainted(true);
		button6.setBorder(new LineBorder(Color.BLACK, 2));
		button6.setBounds(326, 0, 124, 101);
		theTown.add(button6);
		
		JButton button7 = new JButton("");
		button7.setIcon(new ImageIcon("src/mulegame/Images/tree.JPG"));
		button7.setBorderPainted(true);
		button7.setBorder(new LineBorder(Color.BLACK, 2));
		button7.setBounds(0, 174, 75, 76);
		theTown.add(button7);
		
		JButton button4 = new JButton("");
		button4.setIcon(new ImageIcon("src/mulegame/Images/tree2.JPG"));
		button4.setBorderPainted(true);
		button4.setBorder(new LineBorder(Color.BLACK, 2));
		button4.setBounds(189, 203, 75, 36);
		theTown.add(button4);
		
		JButton button8 = new JButton("");
		button8.setIcon(new ImageIcon("src/mulegame/Images/tree.JPG"));
		button8.setBorderPainted(true);
		button8.setBorder(new LineBorder(Color.BLACK, 2));
		button8.setBounds(377, 174, 73, 76);
		theTown.add(button8);
		
		JButton button9 = new JButton("");
		button9.setIcon(new ImageIcon("src/mulegame/Images/Street.JPG"));
		button9.setBorderPainted(true);
		button9.setBorder(null);
		button9.setBounds(0, 105, 155, 63);
		theTown.add(button9);
		
		JButton button10 = new JButton("");
		button10.setIcon(new ImageIcon("src/mulegame/Images/Street.JPG"));
		button10.setBorderPainted(true);
		button10.setBorder(null);
		button10.setBounds(154, 105, 140, 63);
		theTown.add(button10);
		
		JButton button11 = new JButton("");
		button11.setIcon(new ImageIcon("src/mulegame/Images/Street.JPG"));
		button11.setBorderPainted(true);
		button11.setBorder(null);
		button11.setBounds(292, 105, 156, 63);
		theTown.add(button11);
		
		JPanel panelKey = new JPanel();
		panelKey.setBorder(new TitledBorder(null, "Property/Mule Key", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelKey.setBounds(697, 116, 181, 262);
		add(panelKey);
		panelKey.setLayout(null);
		
		JLabel lblProp1 = new JLabel("Town Store : Town Pub");
		lblProp1.setBounds(6, 20, 169, 38);
		panelKey.add(lblProp1);
		lblProp1.setHorizontalAlignment(SwingConstants.CENTER);
		lblProp1.setForeground(Color.BLACK);
		lblProp1.setFont(new Font("MV Boli", Font.BOLD, 12));
		
		JLabel lblProp2 = new JLabel("Land Office : Starbucks");
		lblProp2.setBounds(6, 130, 169, 38);
		panelKey.add(lblProp2);
		lblProp2.setHorizontalAlignment(SwingConstants.CENTER);
		lblProp2.setForeground(Color.BLACK);
		lblProp2.setFont(new Font("MV Boli", Font.BOLD, 12));
		
		JButton btnOwned = new JButton("");
		btnOwned.setIcon(new ImageIcon("src/mulegame/Images/town1.jpg"));
		btnOwned.setBorder(new LineBorder(Color.BLACK, 2));
		btnOwned.setBorderPainted(true);
		btnOwned.setBounds(29, 69, 50, 50);
		panelKey.add(btnOwned);
		
		JButton btnNotOwned = new JButton("");
		btnNotOwned.setIcon(new ImageIcon("src/mulegame/Images/town2.jpg"));
		btnNotOwned.setBorder(new LineBorder(Color.BLACK, 2));
		btnNotOwned.setBorderPainted(true);
		btnNotOwned.setBounds(108, 69, 50, 50);
		panelKey.add(btnNotOwned);
		
		JButton btnParked = new JButton("");
		btnParked.setIcon(new ImageIcon("src/mulegame/Images/town3.jpg"));
		btnParked.setBorder(new LineBorder(Color.BLACK, 2));
		btnParked.setBorderPainted(true);
		btnParked.setBounds(29, 179, 50, 50);
		panelKey.add(btnParked);
		
		JButton btnNotParked = new JButton("");
		btnNotParked.setIcon(new ImageIcon("src/mulegame/Images/town4.jpg"));
		btnNotParked.setBorder(new LineBorder(Color.BLACK, 2));
		btnNotParked.setBorderPainted(true);
		btnNotParked.setBounds(108, 179, 50, 50);
		panelKey.add(btnNotParked);
        
        timeleft = (duration/1000);
        timeleft++;
        timerTick();

	}
	
	@Override
	/**
	 * resets timer based on whether the player made an action on the map and
	 * updates info on the map.
	 */
	public void timerTick(){
	    
	    Integer time = timeleft--;
	    lblTimer.setText(time.toString() + " Seconds");
        invalidate();//this should be the last call in this method
	    
	}
	/**
	 * 
	 */
	@Override
	public void timerTimeOut(){
	     //do nothing, MainScreen's timer will take care of changing the screen   
	}
	
	/**
    * sets the text of txtrPlayer
    * 
    * @param text text to be displayed
    */
    public void setInstructionTextTown(String text) {
        txtrPlayer.setText(text);
        invalidate();
    }
    
    /**
     * creates action listener for back to map button
     * @author AREA 29
     *
     */
    private class BackToMapListener extends AbstractAction{
 	     @Override
 	     public void actionPerformed(ActionEvent evnt){
 	         control.backToMainScreen();   
 	     }
 	}
    
    /**
     * creates action listener for back to map button
     * @author AREA 29
     *
     */
    private class LoadGameListener extends AbstractAction{
 	     @Override
 	     public void actionPerformed(ActionEvent evnt){
 	         control.loadGame(); 
 	     }
 	}
    
    /**
     * creates action listener for Saving Game State
     * @author AREA 29
     *
     */
    private class SaveGameListener extends AbstractAction{
 	     @Override
 	     public void actionPerformed(ActionEvent evnt){
 	         control.saveGame();
 	     }
 	}
    
    /**
     * creates action listener for pub button
     * @author AREA 29
     *
     */
	private class PubAction extends AbstractAction{
	     @Override
	     public void actionPerformed(ActionEvent evnt){
	         control.pubClicked();
	     }
	}
	
	/**
	 * creates action listener for store button
	 * @author AREA 29
	 *
	 */
	private class StoreAction extends AbstractAction{
	     @Override
	     public void actionPerformed(ActionEvent evnt){
	         control.storeClicked();
	     }
	}
}
